import { _decorator, Component, Prefab } from 'cc';
import Biz from '../biz';
import { EntityName } from '../consts/resources';
import { createEnemy } from './entity/enemy';
import { createPlayer } from './entity/player';
const { ccclass } = _decorator;

@ccclass('GameApp')
export class GameApp extends Component {
    private prefabEnemy: { [key: string]: Prefab } = {};

    protected onLoad(): void {}
    init(enemies: Prefab[], bullets: Prefab[]) {
        enemies.forEach((enemy) => {
            this.prefabEnemy[enemy.name] = enemy;
        });
        bullets.forEach((bullet) => {
            Biz.global.bullet[bullet.name] = bullet;
        });
        createPlayer(this.node, this.prefabEnemy[EntityName.Player]);
    }
    protected start(): void {
        this.addEnemy(EntityName.Enemy1);
        this.schedule(() => {
            this.addEnemy(EntityName.Enemy1);
        }, 1);
        this.schedule(() => {
            this.addEnemy(EntityName.Enemy2);
        }, 2);
        this.schedule(() => {
            this.addEnemy(EntityName.Enemy3);
        }, 15);
    }

    private addEnemy(name: string) {
        if (!Biz.global.isStart) return;
        createEnemy(Biz.global.rootEnemy, this.prefabEnemy[name]);
    }
}
